package org.gwt.client;

import java.util.List;

import org.gwt.jaxen.BaseXPath;
import org.gwt.jaxen.JaxenException;
import org.gwt.jaxen.gwtxml.GWTXPath;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Text;
import com.google.gwt.xml.client.XMLParser;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Jaxen4GWTHome implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Button button = new Button("Click me");
		final Label label = new Label();

		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				String xmlString = "<a><b></b><b a='x'>success!</b></a>";

				Document doc = XMLParser.parse(xmlString);

				try {
					BaseXPath xp = new GWTXPath("//b[@a='x']/text()");
					List nodeList = xp.selectNodes(doc);
					Window.alert(((Text) nodeList.get(0)).getData());
				} catch (JaxenException e) {
					e.printStackTrace();
				}
			}
		});

		RootPanel.get("slot1").add(button);

	}
}
